package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.AddressModel;
import com.luck.easyfoodbatch.mvp.model.ShoperModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/10/25
 */
public class ShoperInfoPresenter implements ShoperInfoModel {
    private final String TAG = getClass().getSimpleName();
    private ShoperInfoView shoperInfoView;
    private Context mContext;

    public ShoperInfoPresenter(ShoperInfoView shoperInfoView, Context mContext) {
        this.shoperInfoView = shoperInfoView;
        this.mContext = mContext;
    }


    @Override
    public void shoperInfo(final int type) {
        // type = 1不显示showDialog
        HttpParams params = new HttpParams();
        OkHttpUtils.post(HttpUtil.SHOPER_INFO)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, ShoperModel.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        shoperInfoView.ShoperInfoSuccess((ShoperModel) data);
                        if (type == 0) {
                            shoperInfoView.hideLoading();
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        shoperInfoView.httpStatusError();
                        if (type == 0) {
                            shoperInfoView.hideLoading();
                        }
                    }
                });
    }
}
